<?php
/**
 * Created by PhpStorm.
 * User: dt
 * Date: 2017/9/19
 * Time: 10:50
 */

namespace backend\models;

use Yii;
use backend\base\BaseModel;

class SystemLoginLogModel extends BaseModel
{
    public static function tableName()
    {
        return 'system_login_log';
    }

    /**
     * 保存前
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            $time = time();
            if($this->isNewRecord){
                $this->create_time = $time;
                $this->ip = Yii::$app->request->userIP;
                $this->admin_id = Yii::$app->user->isGuest ? 0 : Yii::$app->user->id;
            }
            return true;
        }
        return false;
    }

    public function getAdmin(){
        return $this->hasOne(AdminModel::className(),['id'=>'admin_id']);
    }

    /**
     * 添加
     * @param $status
     */
    public static function addLog($status){
        $model = new self;
        $model->status = $status;
        $model->save();
    }
}